#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+10;
int n, pre1 = -1, pre2 = -1;
string s1, s2;
string strs[N];
int res = 0x3f3f3f3f;

int main()
{
    cin >> n;
    cin >> s1 >> s2;
    for(int i = 0; i < n; i++) {
        cin >> strs[i];
    }

    for(int i = 0; i < n; i++) {
        if(strs[i] == s1) {
            pre1 = i;
        }
        else if(strs[i] == s2) {
            pre2 = i;
        }
        if(pre1 != -1 && pre2 != -1) {
            res = min(res, abs(pre1 - pre2));
        }
    }
    cout << (res == 0x3f3f3f3f? -1: res);

    return 0;
}